/**
 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
 *
 * Copyright 2021 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mindelec/ccsrc/include/status.h"
#include <map>
#include <sstream>

namespace minddata {
struct Status::Data {
    enum StatusCode status_code = kSuccess;
    std::string status_msg;
    int line_of_code = -1;
    std::string file_name;
    std::string err_description;
};

Status::Status() : data_(std::make_shared<Data>()) {}

Status::Status(enum StatusCode status_code, const std::vector<char> &status_msg) : data_(std::make_shared<Data>()) {
    if (data_ == nullptr) {
      return;
    }

    data_->err_description = CharToString(status_msg);
    data_->status_msg = CharToString(status_msg);
    data_->status_code = status_code;
}

Status::Status(enum StatusCode code, int line_of_code, const char *file_name, const std::vector<char> &extra)
  : data_(std::make_shared<Data>()) {
    if (data_ == nullptr) {
      return;
    }
    data_->status_code = code;
    data_->line_of_code = line_of_code;
    if (file_name != nullptr) {
      data_->file_name = file_name;
    }
    data_->err_description = CharToString(extra);

    std::ostringstream ss;
    #ifndef ENABLE_ANDROID
    #ifdef DEBUG
    ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(code) << ". ";
    #else
    ss << CodeAsString(code) << ". ";
    #endif
    if (!data_->err_description.empty()) {
      ss << data_->err_description;
    }
    ss << "\n";
    #endif

    ss << "Line of code : " << line_of_code << "\n";
    if (file_name != nullptr) {
      ss << "File         : " << file_name << "\n";
    }
    data_->status_msg = ss.str();
}

enum StatusCode Status::StatusCode() const {
if (data_ == nullptr) {
  return kSuccess;
}
return data_->status_code;
}

std::vector<char> Status::ToCString() const {
if (data_ == nullptr) {
  return std::vector<char>();
}
return StringToChar(data_->status_msg);
}

int Status::GetLineOfCode() const {
if (data_ == nullptr) {
  return -1;
}
return data_->line_of_code;
}

std::vector<char> Status::GetErrDescriptionChar() const {
if (data_ == nullptr) {
  return std::vector<char>();
}
return StringToChar(data_->err_description);
}

std::vector<char> Status::CodeAsCString(enum StatusCode c) {
static std::map<enum StatusCode, std::string> info_map = {{StatusCode::kSuccess, "No error occurs."},
    // MD
                                                          {StatusCode::kMDOutOfMemory, "Out of memory"},
                                                          {StatusCode::kMDShapeMisMatch, "Shape is incorrect"},
                                                          {StatusCode::kMDInterrupted, "Interrupted system call"},
                                                          {StatusCode::kMDNoSpace, "No space left on device"},
                                                          {StatusCode::kMDPyFuncException,
                                                          "Exception thrown from PyFunc"},
                                                          {StatusCode::kMDDuplicateKey, "Duplicate key"},
                                                          {StatusCode::kMDPythonInterpreterFailure, ""},
                                                          {StatusCode::kMDTDTPushFailure, "Unexpected error"},
                                                          {StatusCode::kMDFileNotExist, "Unexpected error"},
                                                          {StatusCode::kMDProfilingError, "Profiling Error"},
                                                          {StatusCode::kMDBoundingBoxOutOfBounds,
                                                          "Unexpected error"},
                                                          {StatusCode::kMDBoundingBoxInvalidShape,
                                                          "Unexpected error"},
                                                          {StatusCode::kMDSyntaxError, "Syntax error"},
                                                          {StatusCode::kMDTimeOut, "Unexpected error"},
                                                          {StatusCode::kMDBuddySpaceFull, "BuddySpace full"},
                                                          {StatusCode::kMDNetWorkError, "Network error"},
                                                          {StatusCode::kMDNotImplementedYet, "Unexpected error"},
                                                          {StatusCode::kMDUnexpectedError, "Unexpected error"}};
auto iter = info_map.find(c);
return StringToChar(iter == info_map.end() ? "Unknown error" : iter->second);
}

std::ostream &operator<<(std::ostream &os, const Status &s) {
os << s.ToString();
return os;
}

std::vector<char> Status::SetErrDescription(const std::vector<char> &err_description) {
if (data_ == nullptr) {
  return std::vector<char>();
}
data_->err_description = CharToString(err_description);
std::ostringstream ss;
#ifndef ENABLE_ANDROID
#ifdef DEBUG
ss << "Thread ID " << std::this_thread::get_id() << " " << CodeAsString(data_->status_code) << ". ";
#else
ss << CodeAsString(data_->status_code) << ". ";
#endif
if (!data_->err_description.empty()) {
  ss << data_->err_description;
}
ss << "\n";
#endif

if (data_->line_of_code > 0 && !data_->file_name.empty()) {
  ss << "Line of code : " << data_->line_of_code << "\n";
  ss << "File         : " << data_->file_name << "\n";
}
data_->status_msg = ss.str();
return StringToChar(data_->status_msg);
}

bool Status::operator==(const Status &other) const {
if (data_ == nullptr && other.data_ == nullptr) {
  return true;
}

if (data_ == nullptr || other.data_ == nullptr) {
  return false;
}

return data_->status_code == other.data_->status_code;
}

bool Status::operator==(enum StatusCode other_code) const { return StatusCode() == other_code; }
bool Status::operator!=(const Status &other) const { return !operator==(other); }
bool Status::operator!=(enum StatusCode other_code) const { return !operator==(other_code); }

Status::operator bool() const { return (StatusCode() == kSuccess); }
Status::operator int() const { return static_cast<int>(StatusCode()); }

Status Status::OK() { return StatusCode::kSuccess; }
bool Status::IsOk() const { return (StatusCode() == StatusCode::kSuccess); }
bool Status::IsError() const { return !IsOk(); }
}  // namespace minddata


